//
//  LTHelper.swift
//  UnZip
//
//  Created by lete on 2020/7/15.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit

class LTHelper: NSObject {
    
   static let manage = LTHelper()
    
    /**
        IP地址
     */
    public var ipAddress: String {
        var addresses = [String]()
        var ifaddr : UnsafeMutablePointer<ifaddrs>? = nil
        if getifaddrs(&ifaddr) == 0 {
            var ptr = ifaddr
            while (ptr != nil) {
                let flags = Int32(ptr!.pointee.ifa_flags)
                var addr = ptr!.pointee.ifa_addr.pointee
                if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                        var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                            if let address = String(validatingUTF8:hostname) {
                                addresses.append(address)
                            }
                        }
                    }
                }
                ptr = ptr!.pointee.ifa_next
            }
            freeifaddrs(ifaddr)
        }
        return addresses.first ?? "0.0.0.0"
    }
    
    static func topviewController() -> UIViewController? {
        
        var aRoot = UIApplication.shared.keyWindow?.rootViewController
        
        while aRoot?.presentedViewController != nil {
            aRoot = aRoot?.presentedViewController
            
            if aRoot is UINavigationController {
                aRoot = (aRoot as? UINavigationController)?.visibleViewController
            } else if aRoot is UITabBarController {
                aRoot = (aRoot as? UITabBarController)?.selectedViewController
            }
        }
        
        return aRoot
    }
    
}

//MARK: - 拿到window当前控制器ViewController
struct kNowWindow {
    
   static func kWindow() -> UIWindow{
        let kAppDelegate = UIApplication.shared.delegate as! AppDelegate
        var kvcWindow = kAppDelegate.window
        if #available(iOS 13.0, *)  {
            kvcWindow = UIApplication.shared.keyWindow
        }
        return kvcWindow!
    }
    
}
public extension UIWindow {
    
    func topMostWindowController()->UIViewController? {
        
        var topController = rootViewController
        
        while let presentedController = topController?.presentedViewController {
            topController = presentedController
        }
        
        return topController
    }
    
    func currentViewController()->UIViewController? {
        
        var currentViewController = topMostWindowController()
        if currentViewController is UITabBarController{
            currentViewController = (currentViewController as! UITabBarController).selectedViewController
        }
        while currentViewController != nil && currentViewController is UINavigationController && (currentViewController as! UINavigationController).topViewController != nil {
            currentViewController = (currentViewController as! UINavigationController).topViewController
        }
        
        return currentViewController
    }
}
